package com.example.cleantest;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.my.ads.lib.AdSummary;
import com.my.ads.lib.Config;
import com.my.ads.lib.activity.BaseActivity;
import com.rapid.clean.deep.R;

/**
 * Created by xuanyang on 2018/3/16.
 */

public class PhoneStateActivity extends BaseActivity {

    private ImageView img_back;
    private TextView text_title;
    private RelativeLayout relative_ad_1, relative_ad_2;
    private ScrollView scrollView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_state);
        Log.e("ju","xe");
        initViews();
        start();

    }

    private void start() {
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        if ("clean".equals(getIntent().getStringExtra("type"))) {
            text_title.setText(getResources().getString(R.string.garbage_cleaning));
            scrollView.setBackgroundColor(Color.parseColor("#0c84ff"));
        } else if ("accelerate".equals(getIntent().getStringExtra("type"))) {
            text_title.setText(getResources().getString(R.string.memory_acceleration));
            scrollView.setBackgroundColor(Color.parseColor("#048cef"));
        }

        showAd();
    }

    private void showAd() {
        try {
            AdSummary.getInstance().showBannerAd(relative_ad_1);
            AdSummary.getInstance().showNativeAd(this,relative_ad_2, false);
        } catch (Exception e) {

        }
    }

    private void initViews() {
        img_back = (ImageView) findViewById(R.id.img_back);
        text_title = (TextView) findViewById(R.id.text_title);
        relative_ad_1 = (RelativeLayout) findViewById(R.id.relative_ad_1);
        relative_ad_2 = (RelativeLayout) findViewById(R.id.relative_ad_2);
        scrollView = (ScrollView) findViewById(R.id.scrollView);
    }

    @Override
    public void onResume() {
        super.onResume();
        resume();
    }

    private void resume() {
        SharedPreferences sp_1 = getSharedPreferences("native_ad", Context.MODE_PRIVATE);
        Long show_time = sp_1.getLong("show_native_ad_time", 0L);
        // 刷新原生广告
        if (show_time != 0L && ((System.currentTimeMillis() - show_time) > Integer.valueOf(Config.update_native_ad) * 1000)) {
            try {
                AdSummary.getInstance().showNativeAd(this,relative_ad_2,false);
            } catch (Exception e) {
            }
        }


        SharedPreferences sp_2 = getSharedPreferences("banner_ad", Context.MODE_PRIVATE);
        Long show_time_2 = sp_2.getLong("show_banner_ad_time", 0L);
        // 刷新banner广告
        if (show_time_2 != 0L && ((System.currentTimeMillis() - show_time_2) > Integer.valueOf(Config.update_native_ad) * 1000)) {
            AdSummary.getInstance().showBannerAd(relative_ad_1);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}
